//
//  LimitedTimeDiscountDetailSheet.swift
//  CMB250716
//
//  Created by Assistant on 2024-12-19.
//

import SwiftUI

/// 限时半价优惠详情弹窗
struct LimitedTimeDiscountDetailSheet: View {
    @EnvironmentObject var dataManager: DataManager
    @Environment(\.dismiss) private var dismiss
    @StateObject private var discountManager = LimitedTimeDiscountManager.shared
    @State private var showingMenuView = false
    
    var body: some View {
        NavigationView {
            ZStack {
                // 背景渐变
                LinearGradient(
                    colors: [
                        Color(red: 0.05, green: 0.05, blue: 0.05),
                        Color(red: 0.1, green: 0.1, blue: 0.1),
                        Color(red: 0.15, green: 0.15, blue: 0.15)
                    ],
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
                .ignoresSafeArea()
                
                ScrollView {
                    VStack(spacing: 24) {
                        // 主标题区域
                        VStack(spacing: 16) {
                            // 50% OFF 徽章
                            ZStack {
                                Circle()
                                    .fill(
                                        RadialGradient(
                                            colors: [.yellow, .orange, .red],
                                            center: .center,
                                            startRadius: 10,
                                            endRadius: 60
                                        )
                                    )
                                    .frame(width: 120, height: 120)
                                    .shadow(color: .black.opacity(0.4), radius: 10, x: 0, y: 5)
                                
                                VStack(spacing: 4) {
                                    Text("50%")
                                        .font(.system(size: 36, weight: .black, design: .rounded))
                                        .foregroundColor(.white)
                                    Text(dataManager.currentLanguage == "zh" ? "折扣" : "OFF")
                                        .font(.system(size: 14, weight: .bold))
                                        .foregroundColor(.white.opacity(0.9))
                                }
                            }
                            
                            // 标题和副标题
                            VStack(spacing: 8) {
                                Text(dataManager.currentLanguage == "zh" ? 
                                     AppConfig.LimitedTimeDiscount.promotionTitle.chinese :
                                     AppConfig.LimitedTimeDiscount.promotionTitle.english)
                                    .font(.system(size: 28, weight: .bold, design: .rounded))
                                    .foregroundColor(.white)
                                    .multilineTextAlignment(.center)
                                
                                Text(dataManager.currentLanguage == "zh" ? 
                                     AppConfig.LimitedTimeDiscount.promotionSubtitle.chinese :
                                     AppConfig.LimitedTimeDiscount.promotionSubtitle.english)
                                    .font(.system(size: 16, weight: .medium))
                                    .foregroundColor(.white.opacity(0.8))
                                    .multilineTextAlignment(.center)
                            }
                        }
                        .padding(.top, 20)
                        
                        // 详细信息卡片
                        VStack(spacing: 20) {
                            // 优惠详情
                            detailCard(
                                icon: "percent",
                                title: dataManager.currentLanguage == "zh" ? "优惠详情" : "Discount Details",
                                content: dataManager.currentLanguage == "zh" ? 
                                        AppConfig.LimitedTimeDiscount.promotionDescription.chinese :
                                        AppConfig.LimitedTimeDiscount.promotionDescription.english
                            )
                            
                            // 使用条件
                            detailCard(
                                icon: "list.bullet",
                                title: dataManager.currentLanguage == "zh" ? "使用条件" : "Terms & Conditions",
                                content: dataManager.currentLanguage == "zh" ? 
                                        "• 仅限堂食订单\n• 不可与其他优惠同时使用\n• 优惠期间内有效\n• 部分商品可能不适用" :
                                        "• Dine-in orders only\n• Cannot be combined with other offers\n• Valid during promotion period\n• Some items may be excluded"
                            )
                            
                            // 有效时间
                            detailCard(
                                icon: "clock",
                                title: dataManager.currentLanguage == "zh" ? "有效时间" : "Valid Period",
                                content: dataManager.currentLanguage == "zh" ? 
                                        "营业时间内有效" :
                                        "Valid during business hours"
                            )
                        }
                        .padding(.horizontal, 20)
                        
                        // 行动按钮
                        VStack(spacing: 12) {
                            Button(action: {
                                showingMenuView = true
                                dismiss()
                            }) {
                                HStack {
                                    Image(systemName: "cart.fill")
                                        .font(.system(size: 18, weight: .bold))
                                    Text(dataManager.currentLanguage == "zh" ? "立即订餐" : "Order Now")
                                        .font(.system(size: 18, weight: .bold))
                                }
                                .foregroundColor(.white)
                                .frame(maxWidth: .infinity)
                                .frame(height: 56)
                                .background(
                                    LinearGradient(
                                        colors: [.orange, .red],
                                        startPoint: .leading,
                                        endPoint: .trailing
                                    )
                                )
                                .cornerRadius(16)
                                .shadow(color: .black.opacity(0.3), radius: 8, x: 0, y: 4)
                            }
                            .buttonStyle(PlainButtonStyle())
                            
                            Button(action: {
                                dismiss()
                            }) {
                                Text(dataManager.currentLanguage == "zh" ? "稍后再看" : "Maybe Later")
                                    .font(.system(size: 16, weight: .medium))
                                    .foregroundColor(.white.opacity(0.7))
                            }
                        }
                        .padding(.horizontal, 20)
                        .padding(.bottom, 30)
                    }
                }
            }
            .navigationTitle(dataManager.currentLanguage == "zh" ? "限时优惠" : "Limited Offer")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        dismiss()
                    }) {
                        Image(systemName: "xmark.circle.fill")
                            .font(.system(size: 24))
                            .foregroundColor(.white.opacity(0.7))
                    }
                }
            }
        }
        .fullScreenCover(isPresented: $showingMenuView) {
            MenuView()
                .environmentObject(dataManager)
        }
    }
    
    // MARK: - Helper Views
    
    private func detailCard(icon: String, title: String, content: String) -> some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack(spacing: 12) {
                Image(systemName: icon)
                    .font(.system(size: 20, weight: .bold))
                    .foregroundColor(.orange)
                    .frame(width: 24, height: 24)
                
                Text(title)
                    .font(.system(size: 18, weight: .bold))
                    .foregroundColor(.white)
                
                Spacer()
            }
            
            Text(content)
                .font(.system(size: 14, weight: .regular))
                .foregroundColor(.white.opacity(0.8))
                .lineSpacing(4)
        }
        .padding(20)
        .background(
            RoundedRectangle(cornerRadius: 16)
                .fill(
                    LinearGradient(
                        colors: [
                            Color(red: 0.2, green: 0.2, blue: 0.2),
                            Color(red: 0.15, green: 0.15, blue: 0.15)
                        ],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                )
                .shadow(color: .black.opacity(0.2), radius: 8, x: 0, y: 4)
        )
    }
}

#Preview {
    LimitedTimeDiscountDetailSheet()
        .environmentObject(DataManager.shared)
}